﻿using System;
using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto;
using Best.SmartCms.Dto.Common;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service
{
    /// <summary>
    /// ChannelGroup服务
    /// </summary>
    public class ChannelGroupService : IService
    {
        private readonly ChannelGroupDao _channelGroupDao;
        private readonly WorkContext _workContext;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        /// <param name="workContext"></param>
        public ChannelGroupService(MySqlDbContext mySqlDbContext, WorkContext workContext)
        {
            _workContext = workContext;
            _channelGroupDao = new ChannelGroupDao(mySqlDbContext);
        }

        

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<ChannelGroupListDto> PageList(ChannelGroupListQueryDto query)
        {
            var list = _channelGroupDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<ChannelGroup>, List<ChannelGroupListDto>>(list);
            return new PagedResponse<ChannelGroupListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        /// <summary>
        /// 查找所有
        /// </summary>
        public List<ChannelGroupListDto> FindAll()
        {
            var list = _channelGroupDao.FindAll();
            return TinyMapperExtensions.Map<List<ChannelGroup>, List<ChannelGroupListDto>>(list);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(ChannelGroupEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<ChannelGroupEditDto, ChannelGroup>(dto);
            entity.CreatedTime = DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _channelGroupDao.Add(entity);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(ChannelGroupEditDto dto)
        {
            var entity = _channelGroupDao.GetSingle(dto.Id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{dto.Id}的数据");
            }

            TinyMapperExtensions.Map(dto, entity);
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _channelGroupDao.Update(entity);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ChannelGroupEditDto Info(long id)
        {
            var entity = _channelGroupDao.GetSingle(id);
            return TinyMapperExtensions.Map<ChannelGroup, ChannelGroupEditDto>(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _channelGroupDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _channelGroupDao.Delete(entity);
        }

        /// <summary>
        /// 下拉框绑定
        /// </summary>
        /// <returns></returns>
        public List<SelectOptionDto> Option()
        {
            var list = _channelGroupDao.FindAll();
            return list.OrderBy(p => p.OrderNo).Select(p => new SelectOptionDto { Label = p.Name, Value = p.Id.ToString() }).ToList();
        }

        
    }
}
